package cn.edu.jou.controller;

import cn.edu.jou.common.R;
import cn.edu.jou.entity.User;
import cn.edu.jou.service.UserService;
import cn.edu.jou.utils.SMSUtils;
import cn.edu.jou.utils.ValidateCodeUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 发送验证码短信
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        //获取手机号
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)){
            //生成四位证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code={}",code);
            //调用阿里云提供的api完成短信服务
            //SMSUtils.sendMessage("外卖","",phone,code);
            //将生成的验证码保存到session中
            //session.setAttribute(phone,code);
            //将生成的验证码保存到redis中
            stringRedisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            R.success("发送验证码成功");
        }
        return R.error("出错咯");
    }

    @PostMapping("/login")
    public R<User> login(@RequestBody Map map,HttpSession session,HttpServletRequest request){
        //1.首先进行验证码的比对
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();

        //Object codeSession = session.getAttribute(phone);

        //从缓存中获取验证码
        String redisCode = stringRedisTemplate.opsForValue().get(phone);
        //2.对账号进行校验，如果未注册则注册
        if (redisCode.equals(code)&&redisCode!=null){
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);
            User user = userService.getOne(queryWrapper);

            //如果查询不到该user，则新建
            if (user==null){
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            request.getSession().setAttribute("user",user.getId());
            //登陆成功，删除缓存中验证码
            stringRedisTemplate.delete(phone);
            return R.success(user);
        }

        return R.error("登陆失败");
    }
}
